/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.OvBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.OvBillDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.DataChangedException;
import com.minierp.err.NotAllowException;
import com.minierp.helper.PageHelper;
import com.minierp.model.OvBill;
import com.minierp.model.SystemLog;
import com.minierp.service.OvBillService;
import java.util.Date;

public class OvBillServiceImpl implements OvBillService {
	public void txInsert(OvBillBean form, OvBillDao ovBillDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator)
			throws Exception {
		OvBill obj = form.getOvBill();
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(OvBill.class.getName()));
		if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao,
					form.getCodeRule(), "", form.getOvBill().getTallySetId()));

			for (OvBill d = ovBillDao.get(obj.getBillCode()); d != null; d = ovBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
						.getCodeRule(), "", form.getOvBill().getTallySetId()));
			}
		}

		if (obj.getExamined().equals("Y")) {
			obj.setExamineTime(new Date());
		}

		ovBillDao.save(obj);
	}

	public void txUpdate(OvBillBean form, OvBillDao ovBillDao,
			IdGenerator idGenerator) throws Exception {
		OvBill obj = form.getOvBill();
		Object[] sv = ovBillDao.getStatusValues(obj.getId());
		if (sv == null) {
			throw new Exception("Can\'t get status values for ov bill:"
					+ obj.getId());
		} else {
			long existSycVal = ((Date) sv[0]).getTime();
			String exitEnabled = sv[1].toString();
			String existExamined = sv[2].toString();
			if (existSycVal != form.getSychronizeValue()) {
				throw new DataChangedException("产值单已被其他操作修改，请关闭页面再打开后重新操作！");
			} else if (exitEnabled.equals("N")) {
				throw new NotAllowException("产值单已作废，不能修改！");
			} else if (existExamined.equals("Y")) {
				throw new NotAllowException("产值单已审核，不能修改！");
			} else {
				if (obj.getExamined().equals("Y")) {
					obj.setExamineTime(new Date());
				}

				obj.setUpdateTime(new Date());
				ovBillDao.update(obj);
			}
		}
	}

	public void txExamine(long id, OvBillDao ovBillDao, long examinerId,
			String examinerName) throws Exception {
		OvBill a = (OvBill) ovBillDao.get(OvBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("N")) {
			a.setExamined("Y");
			a.setExamineTime(new Date());
			a.setExaminerId(examinerId);
			a.setExaminerName(examinerName);
			a.setUpdateTime(new Date());
			ovBillDao.update(a);
		}

	}

	public void txUnExamine(long id, OvBillDao ovBillDao) throws Exception {
		OvBill a = (OvBill) ovBillDao.get(OvBill.class, id);
		if (a != null && a.getEnabled().equals("Y")
				&& a.getExamined().equals("Y")) {
			a.setExamined("N");
			a.setExaminerId(0L);
			a.setExaminerName("");
			a.setUpdateTime(new Date());
			ovBillDao.update(a);
		}

	}

	public void txCancel(long id, String reason, OvBillDao ovBillDao,
			UserBean ub, IdGenerator idGenerator) throws Exception {
		OvBill d = (OvBill) ovBillDao.get(OvBill.class, id);
		if (d != null && d.getEnabled().equals("Y")) {
			d.setEnabled("N");
			d.setDisableManId(ub.getUser().getStafferId());
			d.setUpdateTime(new Date());
			ovBillDao.update(d);
			SystemLog log = new SystemLog();
			log.setId(idGenerator.getId(SystemLog.class.getName()));
			log.setActionCode("OV_BILL");
			log.setLogType("CANCEL_LOG");
			log.setCreaterId(ub.getStaffer().getId());
			log.setCreaterName(ub.getStaffer().getStafferName());
			log.setDepName(ub.getDepName());
			log.setDepId(ub.getStaffer().getDepartmentId());
			log.setStatus("S");
			log.setUserName(ub.getUser().getUserName());
			log.setIp(d.getBillCode());
			log.setMark("");
			log.setRelId(id);
			log.setCancelReason(reason);
			log.setTallySetId(d.getTallySetId());
			ovBillDao.save(log);
		}

	}
}